﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.Xml;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Xml.Linq;
using CardClass;

namespace NewSangoBang
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        const int InitialCardsNumber = 4;
        string signal = "";

        //private int Activity = -1;
        // 0 - kill
        // 1 - cure

        private WebClient webClient = new WebClient();
        //StreamResourceInfo zipResourceInfo;
        Random random = new Random();

        CardControlChar cc = new CardControlChar();
        GameHandler Game;
        EventHolder Events = new EventHolder();

        public MainWindow()
        {
            InitializeComponent();
            Game = GameHandler.GetInstance();
            this.Visibility = Visibility.Collapsed;

            Game.module.HasStarted = false;
            Game.module.HasChar = false;
            Game.module.HasRole = false;
            Game.module.PlayerNum = 4;
            Game.module.Activity = -1;
            Game.module.IsHost = "false";

            Game.onJoinResultGiven += new MessageEventHandler(Game_onJoinResultGiven);
            Game.onUserLeaveResultGiven += new MessageEventHandler(Game_onUserLeaveResultGiven);
            Game.onPlayerNameListRefreshed += new MessageEventHandler(Game_onPlayerNameListRefreshed);
            Game.onRoleCardGiven += new MessageEventHandler(Game_onRoleCardGiven);
            Game.onCharacterCardGiven += new MessageEventHandler(Game_onCharacterCardGiven);
            Game.onGameCardDispatched += new MessageEventHandler(Game_onGameCardDispatched);
            Game.onDispatchCardFinish += new MessageEventHandler(Game_onDispatchCardFinish);
            Game.onPlayerActionDo += new MessageEventHandler(Game_onPlayerActionDo);
        }


        void Game_onJoinResultGiven(object sender, List<object> arg)
        {
            //arg[0]: JoinResult
            //arg[1]: detail
            if ((string)arg[0]== "success")
            {
                SucceedJoined(Game.module.Guid);

                // Silverlight control initialize
                //HtmlElement nameElement = HtmlPage.Document.GetElementById("DisplayName");
                Game.module.PlayerName = DisplayName.Text;

                Visibility = Visibility.Visible;
                if (Game.module.IsHost == "true")
                    Btn_Start.Visibility = Visibility.Visible;
                Text_Status.Visibility = Visibility.Visible;
                //ProgressIn.Begin();

            }
            else if ((string)arg[0] == "fail")
            {
                ErrorAlert((string)arg[1]);
                return;
            }
        }

        void Game_onUserLeaveResultGiven(object sender, List<object> arg)
        {
            //arg[0]: LeaveResult
            if ((string)arg[0] == "success")
            {
                signal = "LeaveSucceed";
                ShowMessagePanel("Leave Succeed.");
            }
            else
                ShowMessagePanel("Unknow Error.");
        }

        void Game_onPlayerNameListRefreshed(object sender, List<object> arg)
        {
            //About PlayersPanel...
            if (Game.module.HasStarted)//如果开始游戏后更新列表，只能是有玩家离开……先备份数据。只备份没有离开的，根据新的PlayerList
            {
                Game.module.BackUp.Clear();
                foreach (string name in Game.module.PlayerList)
                {
                    foreach (PlayerPanel pp in PlayersPanel.Items)
                    {
                        if (name == pp.PlayerIDText)
                        {
                            Player player = new Player(name, pp.HPNow, pp.HPFull, pp.CardsNum, pp.Character);
                            Game.module.BackUp.Add(player);
                        }
                    }
                }
            }
            PlayersPanel.Items.Clear();//清空PlayerPanel
            if (Game.module.HasStarted)//再往里填一遍……
            {
                foreach (Player p in Game.module.BackUp)
                    GetPlayer(p.PlayerName, p.Character, p.HPNow, p.HPFull, p.CardNum);
            }
            else
            {
                foreach (string p in Game.module.PlayerList)//若是第一次刷新列表，则都只初始化为0
                    GetPlayer(p, "", 0, 0, 0);
            }
            //About TargetsPanel...
            TargetsPanel.Items.Clear();
            foreach (string p in Game.module.PlayerList)
            {
                AddTextBlock(p);
            }
            AddTextBlock("Cancel");
        }

        void Game_onRoleCardGiven(object sender, List<object> arg)
        {
            GetRole(Game.module.Role, "Role.jpg");
        }

        void Game_onCharacterCardGiven(object sender, List<object> arg)
        {
            foreach (Character c in Game.module.CharacterList)
            {
                foreach (PlayerPanel pp in PlayersPanel.Items)
                {
                    if (pp.PlayerIDText == c.PlayerID)
                    {
                        pp.PlayerIDText = c.PlayerID;
                        pp.Character = c.Charater;
                        pp.HPNow = c.HP;
                        pp.HPFull = c.HP;
                        pp.CardsNum = 4;
                    }
                }
                if (Game.module.PlayerName == c.PlayerID)
                {
                    GetChar(c.Charater, "S:" + c.Description, c.HP, c.Charater +".jpg");
                }
            }
        }

        void Game_onGameCardDispatched(object sender, List<object> arg)
        {
            foreach (string func in Game.module.GameCards)
            {
                GetCards(func, func + ".jpg");
            }
            /*GetCards("kill", "kill.jpg");
            GetCards("kill", "kill.jpg");
            GetCards("cure", "cure.jpg");
            GetCards("miss", "miss.jpg");*/
        }

        void Game_onDispatchCardFinish(object sender, List<object> arg)
        {
            Game.module.HasStarted = true;

            TextPanel.Text = "Ready to start";
            Action.Visibility = Visibility.Visible;

            // functions for user activity
            Text_Status.Text = "Game Starts";
        }

        void Game_onPlayerActionDo(object sender, List<object> arg)
        {
            //arg[0]:action
            XElement action = (XElement)arg[0];
            string actor;
            string target;
            switch (action.Attribute("type").Value)
            {
                case "kill":
                    actor = action.Attribute("player").Value;
                    target = action.Attribute("target").Value;
                    if (Game.module.PlayerName == target)
                    {
                        TextPanel.Text = actor + " is killing you";
                        string sufferMsg = "<message type=\"playerAction\"><action type=\"suffer\"></action></message>";
                        Game.SendMessage(sufferMsg);
                    }
                    else if (Game.module.PlayerName == actor)
                    {
                        TextPanel.Text = "You are killing " + target;
                    }
                    else
                    {
                        TextPanel.Text = actor + " is killing " + target;
                    }
                    break;
                case "cure":
                    actor = action.Attribute("player").Value;
                    target = action.Attribute("target").Value;
                    if (actor == target)
                    {
                        if (actor != Game.module.PlayerName)
                            TextPanel.Text = actor + " is curing himself.";
                        else
                            TextPanel.Text = "You are curing yourself.";
                    }
                    else
                    {
                        if (Game.module.PlayerName == target)
                        {
                            TextPanel.Text = actor + " is curing you.";
                        }
                        else if (Game.module.PlayerName == actor)
                        {
                            TextPanel.Text = "You are curing " + target;
                        }
                        else
                        {
                            TextPanel.Text = actor + " is curing " + target;
                        }
                    }
                    if (Game.module.PlayerName == target)
                    {
                        int newhp = cc.HPNow + 1;
                        if (newhp <= cc.HPFull)
                        {
                            cc.HPNow = newhp;
                            Game.module.HP = newhp;
                            foreach (Character c in Game.module.CharacterList)
                            {
                                if (c.Charater == Game.module.Character)
                                    c.HP = newhp;
                            }
                        }
                    }

                    foreach (PlayerPanel pp in PlayersPanel.Items)
                    {
                        string str = pp.PlayerIDText.Substring(0, pp.PlayerIDText.IndexOf(' '));
                        if (str == target)
                        {
                            int newhp = pp.HPNow + 1;
                            if (newhp <= pp.HPFull)
                            {
                                pp.HPNow = newhp;
                                foreach (Character c in Game.module.CharacterList)
                                {
                                    if (c.Charater == str )
                                        c.HP = newhp;
                                }
                            }
                        }
                    }
                    break;
                case "miss":
                    actor = action.Attribute("player").Value;
                    break;
                case "suffer":
                    actor = action.Attribute("player").Value;
                    if (Game.module.PlayerName == actor)
                    {
                        int newhp = cc.HPNow-1;
                        if (newhp >= 0)
                        {
                            cc.HPNow = newhp;
                            Game.module.HP = newhp;
                            foreach (Character c in Game.module.CharacterList)
                            {
                                if (c.Charater == Game.module.Character)
                                    c.HP = newhp;
                            }
                            if (newhp == 0)
                            {
                                ShowMessagePanel("You are dead...");
                                signal = "GameOver";
                            }
                        }
                    }

                    foreach (PlayerPanel pp in PlayersPanel.Items)
                    {
                        string str = pp.PlayerIDText.Substring(0, pp.PlayerIDText.IndexOf(' '));
                        if (str == actor)
                        {
                            int newhp = pp.HPNow- 1;
                            if (newhp >= 0)
                            {
                                pp.HPNow = newhp;
                                foreach (Character c in Game.module.CharacterList)
                                {
                                    if (c.Charater == str)
                                        c.HP = newhp;
                                }
                            }
                        }
                    }
                    break;
            }
        }


        /*public void GetMessageFromServer(string message)
        {
            XDocument xdocument = XDocument.Parse(message);
            XElement root = xdocument.Element("message");
            Debug.Assert(root.Name == "message");

            IEnumerable<XElement> elements;
            Debug.WriteLine(root.Attribute("type").Value);

            switch (root.Attribute("type").Value)
            {
                case "joinResult":

                    if (root.Attribute("result").Value == "success")
                    {
                        XElement guid = root.Element("guid");
                        SucceedJoined(guid.Value);


                        // Silverlight control initialize
                        //HtmlElement nameElement = HtmlPage.Document.GetElementById("DisplayName");
                        Game.module.PlayerName = DisplayName.Text;
                        Game.module.IsHost = root.Attribute("ishost").Value;

                        Visibility = Visibility.Visible;
                        if (Game.module.IsHost == "true")
                            Btn_Start.Visibility = Visibility.Visible;
                        Text_Status.Visibility = Visibility.Visible;
                        //ProgressIn.Begin();

                    }
                    else if (root.Attribute("result").Value == "fail")
                    {
                        XElement detail = root.Element("detail");
                        ErrorAlert(detail.Value);
                        return;
                    }
                    break;
                case "playerList":
                    elements =
                        from el in root.Elements("player")
                        select el;

                    //Clear Players
                    Game.module.PlayerList.Clear();
                    PlayersPanel.Items.Clear();
                    Game.module.PlayerNum = 0;

                    foreach (XElement element in elements)
                    {
                        string playername = element.Value;

                        if (!Game.module.PlayerList.Contains(playername))
                        {
                            GetPlayer(playername, "HP:", "Cards:");
                            Game.module.PlayerList.Add(playername);
                            Game.module.PlayerNum++;

                            TextBlock tb = new TextBlock();
                            tb.TextDecorations = TextDecorations.Underline;
                            tb.Text = playername;
                            tb.Cursor = Cursors.Hand;
                            tb.MouseLeftButtonUp += new MouseButtonEventHandler(ActPerson_MouseLeftButtonUp);
                            tb.Foreground = new SolidColorBrush(Colors.Green);
                            tb.SetValue(Canvas.LeftProperty, 5.0);
                            tb.SetValue(Canvas.TopProperty, (Game.module.PlayerNum - 1) * 20.0);
                            //UserLayer.Children.Add(tb);
                            bool flag = true;
                            foreach (TextBlock item in TargetsPanel.Items)
                            {
                                if (tb.Text == item.Text)
                                    flag = false;
                            }
                            if (flag)
                                TargetsPanel.Items.Add(tb);
                        }
                    }
                    break;

                case "startGameResult":
                    string result = root.Attribute("result").Value;
                    break;

                case "roleCard":
                    XElement role = root.Element("role");
                    GetRole(role.Value, "longda.png");
                    break;

                case "characterCard":
                    elements =
                        from el in root.Elements("character")
                        select el;
                    foreach (XElement element in elements)
                    {
                        var player = element.Attribute("player").Value;
                        var hp = element.Attribute("hp").Value;
                        var description = element.Attribute("description").Value;

                        // Update player list
                        foreach (PlayerPanel pp in PlayersPanel.Items)
                        {
                            if (pp.PlayerIDText == player)
                            {
                                pp.PlayerIDText = player + "   C:" + element.Value;
                                pp.HPText = "HP: " + hp + "/" + hp;
                                pp.CardsText = "Cards: 4";
                            }
                        }

                        if (Game.module.PlayerName == player)
                        {
                            GetChar(element.Value, "S:" + description, "HP: " + hp + "/" + hp, "yangweili.png");
                        }
                    }

                    break;

                case "gameCard":
                    elements =
                        from el in root.Elements("gameCard")
                        select el;
                    foreach (XElement element in elements)
                    {
                        GetCards(element.Value, element.Value + ".png");
                    }

                    break;

                case "dispatchCardFinish":
                    Game.module.HasStarted = true;
                    TextBlock t = new TextBlock();
                    t.TextDecorations = TextDecorations.Underline;
                    t.Foreground = new SolidColorBrush(Colors.Purple);
                    t.Text = "Cancel";
                    t.MouseLeftButtonUp += new MouseButtonEventHandler(ActPerson_MouseLeftButtonUp);
                    t.Cursor = Cursors.Hand;
                    t.SetValue(Canvas.LeftProperty, 5.0);
                    t.SetValue(Canvas.TopProperty, Game.module.PlayerNum * 20.0);
                    TargetsPanel.Items.Add(t);

                    TextPanel.Text = "Ready to start";
                    Action.Visibility = Visibility.Visible;

                    // functions for user activity
                    ActKill.MouseLeftButtonUp += new MouseButtonEventHandler(ActKill_MouseLeftButtonUp);
                    ActCure.MouseLeftButtonUp += new MouseButtonEventHandler(ActCure_MouseLeftButtonUp);
                    Text_Status.Text = "Game Starts";

                    //ActionPanelIn.Begin();         //??
                    break;

                case "playerAction":
                    XElement action = root.Element("action");
                    string actor;
                    string target;
                    switch (action.Attribute("type").Value)
                    {
                        case "kill":
                            actor = action.Attribute("player").Value;
                            target = action.Attribute("target").Value;
                            if (Game.module.PlayerName == target)
                            {
                                TextPanel.Text = actor + " is killing you";
                                //
                                string sufferMsg = "<message type=\"playerAction\"><action type=\"suffer\"></action></message>";
                                Game.SendMessage(sufferMsg);
                            }
                            else if (Game.module.PlayerName == actor)
                            {
                                TextPanel.Text = "you are killing " + target;
                            }
                            else
                            {
                                TextPanel.Text = actor + " is killing " + target;
                            }
                            break;
                        case "cure":
                            actor = action.Attribute("player").Value;
                            target = action.Attribute("target").Value;
                            break;
                        case "miss":
                            actor = action.Attribute("player").Value;
                            break;
                        case "suffer":
                            actor = action.Attribute("player").Value;
                            if (Game.module.PlayerName == actor)
                            {
                                string[] b = cc.HPText.Substring(4).Split('/');
                                cc.HPText = "HP: " + (Int32.Parse(b[0]) - 1).ToString() + "/" + b[1];
                            }

                            foreach (PlayerPanel pp in PlayersPanel.Items)
                            {
                                string str = pp.PlayerIDText.Substring(0, pp.PlayerIDText.IndexOf(' '));
                                if (str == actor)
                                {
                                    string[] b = pp.HPText.Substring(4).Split('/');
                                    pp.HPText = "HP: " + (Int32.Parse(b[0]) - 1).ToString() + "/" + b[1];
                                }
                            }

                            break;
                    }

                    break;
                case "userLeaveResult":
                    if (root.Attribute("result").Value == "success")
                    {
                        this.Visibility = Visibility.Collapsed;
                    }

                    break;

            }
        }*/


        //Methods to get the game initialized...

        private void GetRole(string role, string image)
        {
            if (Game.module.HasRole)
                return;
            // Role Card Distribution
            CardControlRole cr = new CardControlRole();
            cr.RoleText = role; //

            cr.SetURI(image); // 

            cr.Left = 510;
            cr.Top = 90;
            CardsPanel.Children.Add(cr);
            Game.module.HasRole = true;
        }

        private void GetChar(string charactor, string skill, int hp, string image)
        {
            if (Game.module.HasChar)
                return;
            // Character Card Distribution
            cc.CharText = charactor; // 
            cc.SkillText = skill; // 
            cc.HPFull = hp;
            cc.HPNow = hp; // 
            cc.SetURI(image); //

            cc.Left = 410;
            cc.Top = 70;
            //cc.Angle = (r.NextDouble() - 0.5) * 60;

            CardsPanel.Children.Add(cc);
            Game.module.HasChar = true;
        }

        private void GetCards(string func, string image/*, int index*/)
        {
            // Character Card Distribution

            CardControlAction cc = new CardControlAction();
            cc.funcText = func;
            cc.SetURI(image);

            cc.Left = random.NextDouble() * 350;
            cc.Top = random.NextDouble() * 80;
            cc.Angle = (random.NextDouble() - 0.5) * 60;

            CardsPanel.Children.Add(cc);
        }

        private void GetPlayer(string playerid, string character, int hpnow, int hpfull, int cardnum)
        {
            PlayerPanel pr = new PlayerPanel();
            pr.PlayerIDText = playerid;
            pr.Character = character;
            pr.HPNow = hpnow;
            pr.HPFull = hpfull;
            pr.CardsNum = cardnum;

            pr.Left = 0;
            pr.Top = 0;

            PlayersPanel.Items.Add(pr);
        }

        //All kinds of events...

        private void Btn_Register_Click(object sender, RoutedEventArgs e)
        {
                
                if (DisplayName.Text != "")
                {
                    string yourname = this.DisplayName.Text;
                    GameHandler.RegisterReturns r = Game.Join(yourname);
                
                    if (r == GameHandler.RegisterReturns.Success)
                    {
                        ShowMessagePanel("Join Succeed.");
                    }
                    else if (r == GameHandler.RegisterReturns.UsedPlayerName)
                        ShowMessagePanel("This name has been used, please input another one.");                        
                    else
                        ShowMessagePanel("Unknown error.");
                }
                else
                    ShowMessagePanel("Please input your player name.");

        }

        private void Btn_Leave_Click(object sender, RoutedEventArgs e)
        {
            ShowConfirmPanel("Are you sure to leave?");
        }

        void ActPerson_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            if (tb.Text == "Cancel")
            {
                Game.module.Activity = -1;
                TextPanel.Text = "Cancel";
                UserLayer.Visibility = Visibility.Collapsed;
                return;
            }

            String messageStr;
            switch (Game.module.Activity)
            {
                case 0:
                    if (tb.Text == Game.module.PlayerName)
                    {
                        TextPanel.Text = "Cannot kill yourself";
                        UserLayer.Visibility = Visibility.Collapsed;
                        return;
                    }
                    RemoveCard("Kill");
                    ChangeMyCardNum();
                    messageStr = "<message type=\"playerAction\"><action type=\"kill\" target=\"" + tb.Text + "\"></action></message>";
                    Game.SendMessage(messageStr);

                    break;

                case 1:
                    RemoveCard("Cure");
                    ChangeMyCardNum();
                    messageStr = "<message type=\"playerAction\"><action type=\"cure\" target=\"" + tb.Text + "\"></action></message>";
                    Game.SendMessage(messageStr);

                    break;
            }
            UserLayer.Visibility = Visibility.Collapsed;
            return;

        }


        private void ActKill_Click(object sender, RoutedEventArgs e)
        {
            if (!Game.module.HasStarted)
                return;
            bool flag = false;
            Button btn = (Button)sender;
            foreach (var card in CardsPanel.Children)
            {
                CardControlAction actioncard = card as CardControlAction;
                if (actioncard == null)
                    continue;
                if (actioncard.funcText == "Kill")
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                TextPanel.Text = "No Kill in your cards";
                return;
            }


            ClearActs();
            Game.module.Activity = 0;
            ActKill.Foreground = new SolidColorBrush(Colors.Red);
            UserLayer.SetValue(Canvas.LeftProperty, Mouse.GetPosition(null).X);
            UserLayer.SetValue(Canvas.TopProperty, Mouse.GetPosition(null).Y);
            UserLayer.SetValue(Canvas.ZIndexProperty, 10);
            UserLayer.Visibility = Visibility.Visible;

            foreach (var findMe in TargetsPanel.Items)
            {
                TextBlock tb = findMe as TextBlock;
                if (tb == null)
                    continue;
                if (tb.Text == Game.module.PlayerName)
                    tb.Foreground = new SolidColorBrush(Colors.Gray);
                else if (tb.Text == "cancel")
                    tb.Foreground = new SolidColorBrush(Colors.Purple);
                else
                    tb.Foreground = new SolidColorBrush(Colors.Green);
            }
        }

        private void ActCure_Click(object sender, RoutedEventArgs e)
        {
            if (!Game.module.HasStarted)
                return;
            bool flag = false;
            Button btn = (Button)sender;
            foreach (var card in CardsPanel.Children)
            {
                CardControlAction actioncard = card as CardControlAction;
                if (actioncard == null)
                    continue;
                if (actioncard.funcText == "Cure")
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                TextPanel.Text = "No Cure in your cards";
                return;
            }

            ClearActs();
            Game.module.Activity = 1;
            ActCure.Foreground = new SolidColorBrush(Colors.Green);
            UserLayer.SetValue(Canvas.LeftProperty, Mouse.GetPosition(null).X);
            UserLayer.SetValue(Canvas.TopProperty, Mouse.GetPosition(null).Y);
            UserLayer.SetValue(Canvas.ZIndexProperty, 10);
            UserLayer.Visibility = Visibility.Visible;

            foreach (var findMe in TargetsPanel.Items)
            {
                TextBlock tb = findMe as TextBlock;
                if (tb == null)
                    continue;
                if (tb.Text == "cancel")
                    tb.Foreground = new SolidColorBrush(Colors.Purple);
                else
                    tb.Foreground = new SolidColorBrush(Colors.Green);
            }
        }

        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            switch (signal)
            {
                case "": break;
                case "GameOver":
                    Game.SendMessage("<message type=\"userLeave\"/>");
                    break;
                case "LeaveSucceed":
                    this.Close();
                    break;
            }
            signal = "";
            MessagePanel.Visibility = Visibility.Collapsed;
        }

        private void BtnYes_Click(object sender, RoutedEventArgs e)
        {
            Game.SendMessage("<message type=\"userLeave\"/>");
            ConfirmPanel.Visibility = Visibility.Collapsed;
        }

        private void BtnNo_Click(object sender, RoutedEventArgs e)
        {
            ConfirmPanel.Visibility = Visibility.Collapsed;
        }

        //Other methods for details...

        void ClearActs()
        {
            ActKill.Foreground = new SolidColorBrush(Colors.Black);
            ActCure.Foreground = new SolidColorBrush(Colors.Black);
        }

        private void StartGame(object o, EventArgs e)
        {
            if (Game.module.HasStarted)
                return;
            Btn_Start.Visibility = Visibility.Collapsed;
            Game.SendMessage(QueryString("startGame"));
        }

        private string QueryString(string type)
        {
            return "<message type=\"" + type + "\"></message>";
        }

        private void ShowMessagePanel(string message)
        {
            TbMessage.Text = message;
            UserLayer.Visibility = Visibility.Collapsed;

            MessagePanel.Visibility = Visibility.Visible;
        }

        private void ShowConfirmPanel(string message)
        {
            TbConfirm.Text = message;
            UserLayer.Visibility = Visibility.Collapsed;
            ConfirmPanel.Visibility = Visibility.Visible;
        }

        public void SucceedJoined(string args)
        {
            Btn_Leave.IsEnabled = true;
            Btn_Register.IsEnabled = false;
        }

        public void ErrorAlert(string args)
        {
            ShowMessagePanel(args);
        }

        public void RemoveCard(string fun)
        {
            foreach (var card in CardsPanel.Children)
            {
                CardControlAction actioncard = card as CardControlAction;
                if (actioncard == null)
                    continue;
                if (actioncard.funcText == fun)
                {
                    CardsPanel.Children.Remove(actioncard);
                    break;
                }
            }
        }

        public void ChangeMyCardNum()
        {
            foreach (PlayerPanel pp in PlayersPanel.Items)
            {
                string str = pp.PlayerIDText;
                if (str == Game.module.PlayerName)
                    pp.CardsNum--;
            }
        }

        public void AddTextBlock(string item)
        {
            TextBlock tb = new TextBlock();
            tb.TextDecorations = TextDecorations.Underline;
            tb.Text = item;
            tb.Cursor = Cursors.Hand;
            tb.MouseLeftButtonUp += new MouseButtonEventHandler(ActPerson_MouseLeftButtonUp);
            tb.Foreground = new SolidColorBrush(Colors.Green);
            tb.SetValue(Canvas.LeftProperty, 5.0);
            if (item != "Cancel")
                tb.SetValue(Canvas.TopProperty, (Game.module.PlayerNum - 1) * 20.0);
            else
                tb.SetValue(Canvas.TopProperty, Game.module.PlayerNum* 20.0);
            TargetsPanel.Items.Add(tb);
        }
    }
}
